package utils;

import FCRendering.FCRConstants;
import FCRendering.FCRMaster;
import Function.FCMutilPlayCache;
import VWorldGameStudio.*;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import static FCRendering.FCRMaster.*;

public class FCMixUtil {

    public static void resetTankNameList() {
        FCRMaster.fcv.getTanknameList().clear();
        FCRMaster.fcv.getTankHostList().clear();
        if (FCRMaster.player1.getPlayertank1() != null && !FCRMaster.player1.getPlayertank1().equals("null")) {
            FCRMaster.fcv.getTanknameList().add(FCRMaster.player1.getPlayertank1());
            FCRMaster.fcv.getTankHostList().add(FCRMaster.player1.getPlayertankHostId());
        }
        for (FCRPlayerLink link : FCMutilPlayCache.getTeamMembers()) {
            if (link.curPlayerHostUID.equals(FCRMaster.gameSave.HOST_UID)) {
                FCPlayerInfo p = FCMutilPlayCache.findPlayerByName(link.curPlayerHostUID, link.curPlayerName);
                if (p != null && p.getPlayertank1() != null && !p.getPlayertank1().equals("null")) {
                    FCRMaster.fcv.getTanknameList().add(p.getPlayertank1());
                    FCRMaster.fcv.getTankHostList().add(p.getPlayertankHostId());
                }
            }
        }
        for (FCPropulsion propulsion : fcv.getPropulsions()) {
            if (propulsion.getPropulsionHostId().equals(FCRMaster.gameSave.HOST_UID)) {
                FCRMaster.fcv.getTanknameList().add(propulsion.getPropulsionName());
                FCRMaster.fcv.getTankHostList().add(propulsion.getPropulsionHostId());
            }
        }
        FCRMaster.fcv.setShowTankMenuTankIndex(0);
    }



    /**
     * 非战斗中，按Q切换人物的背包信息
     */
    public static void changeShowPlayerBagByKey() {
        ArrayList<String> names = new ArrayList<>();
        names.add(player1.getName());
        for (FCRPlayerLink link : FCMutilPlayCache.getTeamMembers()) {
            if (link.curPlayerHostUID.equals(FCRMaster.gameSave.HOST_UID)) {
                names.add(link.curPlayerName);
            }
        }
        int index = names.indexOf(fcv.getShowWhosBagItem());
        if (index < 0) {
            index = 0;
        } else {
            index++;
            if (index >= names.size()) {
                index = 0;
            }
        }
        fcv.setShowWhosBagItem(names.get(index));
    }



    //因为同步到别的玩家，肯定是没有原来的那个克隆物品的，只能用原版的
    private static String splitTankItemName(String itemId) {
        if (!itemId.contains(",")) {
            return itemId;
        } else {
            return itemId.split(",")[0];
        }
    }


    /**
     * 检查坦克有没中酸，中了就扣甲
     * 规则，每触发一次，扣掉最大值的0.5%甲，依次扣顺序是，先扣
     * 整体防甲，到底盘，主炮，副炮，SE，扣到全损
     *
     * @param tank
     */
    public static void stepCheckAcidAndSubDefense(FCTank tank) {
        if (tank.isAcidState()) {
            int value = 0;
            if (tank.getDefense() > 0) {
                value = (int) (tank.getDefense() - (5 + tank.getMaxDefense() * 0.005));
                value = Math.max(0, value);
                tank.setDefense(value);

                return;
            }
            //TODO 如果不是本机的，扣掉下面的值没什么用，因为扣的都是克隆对象的甲值，跳出
            if (doneSubFCItemDefens(tank, tank.getChassisID())) {
                tank.reInitShuxing();
                return;
            }
            if (doneSubFCItemDefens(tank, tank.getGunID())) {
                tank.reInitShuxing();
                return;
            }
            if (doneSubFCItemDefens(tank, tank.getGun2ID())) {
                tank.reInitShuxing();
                return;
            }
            if (doneSubFCItemDefens(tank, tank.getSEID())) {
                tank.reInitShuxing();
                return;
            }
            if (doneSubFCItemDefens(tank, tank.getRadarID())) {
                tank.reInitShuxing();
                return;
            }
            if (doneSubFCItemDefens(tank, tank.getEngineID())) {
                tank.reInitShuxing();
                return;
            }
            if (doneSubFCItemDefens(tank, tank.getCdeviceID())) {
                tank.reInitShuxing();
                return;
            }
        }
    }

    private static boolean doneSubFCItemDefens(FCTank tank, String itemName) {
        FCItem item = itemLib.getItemOfID(itemName);
        if (item != null) {
            int value = 0;
            if (item.getCurrentDefense() > 0) {
                value = (int) (item.getCurrentDefense() - (1 + item.getDefenseAdd() * 0.01));
                value = Math.max(0, value);
                item.setCurrentDefense(value);
                return true;
            }
        }
        return false;
    }


    //TODO 几率获取鸟粪或者铅竹
    public static void mayBeGetReduice(FCTank tank) {
        if (tank != null) {
            Random random = new Random();
            if (random.nextFloat() > 0.97f) {//概率得
                if(random.nextFloat() > 0.6){
                    if(!tank.addItemToKnapsack(FCRConstants.QianZhuName)) {
                        tank.getResidues().add(FCRConstants.QianZhuName);
                    }
                    System.out.println("战车获得了铅块");
                }else {
                    if(!tank.addItemToKnapsack(FCRConstants.BridShitName)) {
                        tank.getResidues().add(FCRConstants.BridShitName);
                    }
                    System.out.println("战车获得了鸟粪");
                }
                tank.reInitShuxing();
            }
        }
    }

    /**
     * 压缩字符串
     * @param str 要压缩的字符串
     * @return 压缩后的字符串
     */
    public static String compress(String str) {
        if (str == null || str.length() == 0) {
            return str;
        }
        GZIPOutputStream gzipOutputStream=null;
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try {
            gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream);
            gzipOutputStream.write(str.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (gzipOutputStream != null) {
                try {
                    gzipOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return new sun.misc.BASE64Encoder().encode(byteArrayOutputStream.toByteArray());
    }

    /**
     * 解压缩
     * @param compressedStr 压缩后的字符串
     * @return 解压后的字符串
     */
    public static String uncompress(String compressedStr) {
        if (compressedStr == null) {
            return null;
        }
        byte[] compressed;
        String decompressed=null;
        ByteArrayInputStream byteArrayInputStream = null;
        GZIPInputStream gzipInputStream = null;
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try {
            compressed = new sun.misc.BASE64Decoder().decodeBuffer(compressedStr);
            byteArrayInputStream = new ByteArrayInputStream(compressed);
            gzipInputStream = new GZIPInputStream(byteArrayInputStream);
            byte[] buffer = new byte[1024];
            int offset;
            while ((offset = gzipInputStream.read(buffer)) != -1) {
                byteArrayOutputStream.write(buffer, 0, offset);
            }
            decompressed = byteArrayOutputStream.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (gzipInputStream != null) {
                try {
                    gzipInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (byteArrayInputStream != null) {
                try {
                    byteArrayInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            try {
                byteArrayOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return decompressed;
    }
}
